/*------------------------------------------------------------------------*/
/*  Copyright 2014 National Renewable Energy Laboratory.                  */
/*  This software is released under the license detailed                  */
/*  in the file, LICENSE, which is located in the top-level Nalu          */
/*  directory structure                                                   */
/*------------------------------------------------------------------------*/

#include "kernels/UnitTestKernelUtils.h"
#include "UnitTestUtils.h"
#include "UnitTestHelperObjects.h"

#include "nso/MomentumNSOKeElemKernel.h"

namespace  {
namespace hex8_golds {
namespace nso_ke {
  static constexpr double rhs[24] = {
    -0.0063707777161640, 0.0063707777161640, 0.0000000000000000,
    -0.0086081947887376, -0.0106799702967436, 0.0000000000000000,
    0.0042990022081580, -0.0042990022081580, 0.0000000000000000,
    0.0106799702967436, 0.0086081947887376, 0.0000000000000000,
    -0.0063707777161640, 0.0063707777161640, 0.0000000000000000,
    -0.0086081947887376, -0.0106799702967436, 0.0000000000000000,
    0.0042990022081580, -0.0042990022081580, 0.0000000000000000,
    0.0106799702967436, 0.0086081947887376, 0.0000000000000000
  };


  static constexpr double lhs[24][24] = {
    {
        0.0111590073087890, 0.0000000000000000, 0.0000000000000000,
        -0.0037196691029297, 0.0000000000000000, 0.0000000000000000,
        -0.0037196691029297, 0.0000000000000000, 0.0000000000000000,
        -0.0037196691029297, 0.0000000000000000, 0.0000000000000000,
        0.0037196691029297, 0.0000000000000000, 0.0000000000000000,
        -0.0012398897009766, 0.0000000000000000, 0.0000000000000000,
        -0.0012398897009766, 0.0000000000000000, 0.0000000000000000,
        -0.0012398897009766, 0.0000000000000000, 0.0000000000000000,
    },
    {
        0.0000000000000000, 0.0111590073087890, 0.0000000000000000,
        0.0000000000000000, -0.0037196691029297, 0.0000000000000000,
        0.0000000000000000, -0.0037196691029297, 0.0000000000000000,
        0.0000000000000000, -0.0037196691029297, 0.0000000000000000,
        0.0000000000000000, 0.0037196691029297, 0.0000000000000000,
        0.0000000000000000, -0.0012398897009766, 0.0000000000000000,
        0.0000000000000000, -0.0012398897009766, 0.0000000000000000,
        0.0000000000000000, -0.0012398897009766, 0.0000000000000000,
    },
    { 0.0000000000000000, 0.0000000000000000, 0.0111590073087890,
        0.0000000000000000, 0.0000000000000000, -0.0037196691029297,
        0.0000000000000000, 0.0000000000000000, -0.0037196691029297,
        0.0000000000000000, 0.0000000000000000, -0.0037196691029297,
        0.0000000000000000, 0.0000000000000000, 0.0037196691029297,
        0.0000000000000000, 0.0000000000000000, -0.0012398897009766,
        0.0000000000000000, 0.0000000000000000, -0.0012398897009766,
        0.0000000000000000, 0.0000000000000000, -0.0012398897009766,
    },
    {
        -0.0003352301190575, 0.0000000000000000, 0.0000000000000000,
        0.0213123242604053, 0.0000000000000000, 0.0000000000000000,
        -0.0033376661125084, 0.0000000000000000, 0.0000000000000000,
        -0.0035923347727893, 0.0000000000000000, 0.0000000000000000,
        -0.0036235166870317, 0.0000000000000000, 0.0000000000000000,
        -0.0034312118552358, 0.0000000000000000, 0.0000000000000000,
        -0.0046243286848487, 0.0000000000000000, 0.0000000000000000,
        -0.0023680360289339, 0.0000000000000000, 0.0000000000000000,
    },
    {
        0.0000000000000000, -0.0003352301190575, 0.0000000000000000,
        0.0000000000000000, 0.0213123242604053, 0.0000000000000000,
        0.0000000000000000, -0.0033376661125084, 0.0000000000000000,
        0.0000000000000000, -0.0035923347727893, 0.0000000000000000,
        0.0000000000000000, -0.0036235166870317, 0.0000000000000000,
        0.0000000000000000, -0.0034312118552358, 0.0000000000000000,
        0.0000000000000000, -0.0046243286848487, 0.0000000000000000,
        0.0000000000000000, -0.0023680360289339, 0.0000000000000000,
    },
    {
        0.0000000000000000, 0.0000000000000000, -0.0003352301190575,
        0.0000000000000000, 0.0000000000000000, 0.0213123242604053,
        0.0000000000000000, 0.0000000000000000, -0.0033376661125084,
        0.0000000000000000, 0.0000000000000000, -0.0035923347727893,
        0.0000000000000000, 0.0000000000000000, -0.0036235166870317,
        0.0000000000000000, 0.0000000000000000, -0.0034312118552358,
        0.0000000000000000, 0.0000000000000000, -0.0046243286848487,
        0.0000000000000000, 0.0000000000000000, -0.0023680360289339,
    },
    {
        -0.0052208870996551, 0.0000000000000000, 0.0000000000000000,
        -0.0052208870996551, 0.0000000000000000, 0.0000000000000000,
        0.0156626612989653, 0.0000000000000000, 0.0000000000000000,
        -0.0052208870996551, 0.0000000000000000, 0.0000000000000000,
        -0.0017402956998850, 0.0000000000000000, 0.0000000000000000,
        -0.0017402956998850, 0.0000000000000000, 0.0000000000000000,
        0.0052208870996551, 0.0000000000000000, 0.0000000000000000,
        -0.0017402956998850, 0.0000000000000000, 0.0000000000000000,
    },
    { 0.0000000000000000, -0.0052208870996551, 0.0000000000000000,
        0.0000000000000000, -0.0052208870996551, 0.0000000000000000,
        0.0000000000000000, 0.0156626612989653, 0.0000000000000000,
        0.0000000000000000, -0.0052208870996551, 0.0000000000000000,
        0.0000000000000000, -0.0017402956998850, 0.0000000000000000,
        0.0000000000000000, -0.0017402956998850, 0.0000000000000000,
        0.0000000000000000, 0.0052208870996551, 0.0000000000000000,
        0.0000000000000000, -0.0017402956998850, 0.0000000000000000,
    },
    { 0.0000000000000000, 0.0000000000000000, -0.0052208870996551,
        0.0000000000000000, 0.0000000000000000, -0.0052208870996551,
        0.0000000000000000, 0.0000000000000000, 0.0156626612989653,
        0.0000000000000000, 0.0000000000000000, -0.0052208870996551,
        0.0000000000000000, 0.0000000000000000, -0.0017402956998850,
        0.0000000000000000, 0.0000000000000000, -0.0017402956998850,
        0.0000000000000000, 0.0000000000000000, 0.0052208870996551,
        0.0000000000000000, 0.0000000000000000, -0.0017402956998850,
    },
    { -0.0003352301190575, 0.0000000000000000, 0.0000000000000000,
        -0.0035923347727893, 0.0000000000000000, 0.0000000000000000,
        -0.0033376661125084, 0.0000000000000000, 0.0000000000000000,
        0.0213123242604053, 0.0000000000000000, 0.0000000000000000,
        -0.0036235166870317, 0.0000000000000000, 0.0000000000000000,
        -0.0023680360289339, 0.0000000000000000, 0.0000000000000000,
        -0.0046243286848487, 0.0000000000000000, 0.0000000000000000,
        -0.0034312118552358, 0.0000000000000000, 0.0000000000000000,
    },
    {
        0.0000000000000000, -0.0003352301190575, 0.0000000000000000,
        0.0000000000000000, -0.0035923347727893, 0.0000000000000000,
        0.0000000000000000, -0.0033376661125084, 0.0000000000000000,
        0.0000000000000000, 0.0213123242604053, 0.0000000000000000,
        0.0000000000000000, -0.0036235166870317, 0.0000000000000000,
        0.0000000000000000, -0.0023680360289339, 0.0000000000000000,
        0.0000000000000000, -0.0046243286848487, 0.0000000000000000,
        0.0000000000000000, -0.0034312118552358, 0.0000000000000000,
    },
    { 0.0000000000000000, 0.0000000000000000, -0.0003352301190575,
        0.0000000000000000, 0.0000000000000000, -0.0035923347727893,
        0.0000000000000000, 0.0000000000000000, -0.0033376661125084,
        0.0000000000000000, 0.0000000000000000, 0.0213123242604053,
        0.0000000000000000, 0.0000000000000000, -0.0036235166870317,
        0.0000000000000000, 0.0000000000000000, -0.0023680360289339,
        0.0000000000000000, 0.0000000000000000, -0.0046243286848487,
        0.0000000000000000, 0.0000000000000000, -0.0034312118552358,
    },
    {
        0.0037196691029297, 0.0000000000000000, 0.0000000000000000,
        -0.0012398897009766, 0.0000000000000000, 0.0000000000000000,
        -0.0012398897009766, 0.0000000000000000, 0.0000000000000000,
        -0.0012398897009766, 0.0000000000000000, 0.0000000000000000,
        0.0111590073087890, 0.0000000000000000, 0.0000000000000000,
        -0.0037196691029297, 0.0000000000000000, 0.0000000000000000,
        -0.0037196691029297, 0.0000000000000000, 0.0000000000000000,
        -0.0037196691029297, 0.0000000000000000, 0.0000000000000000,
    },
    {
        0.0000000000000000, 0.0037196691029297, 0.0000000000000000,
        0.0000000000000000, -0.0012398897009766, 0.0000000000000000,
        0.0000000000000000, -0.0012398897009766, 0.0000000000000000,
        0.0000000000000000, -0.0012398897009766, 0.0000000000000000,
        0.0000000000000000, 0.0111590073087890, 0.0000000000000000,
        0.0000000000000000, -0.0037196691029297, 0.0000000000000000,
        0.0000000000000000, -0.0037196691029297, 0.0000000000000000,
        0.0000000000000000, -0.0037196691029297, 0.0000000000000000,
    },
    {
        0.0000000000000000, 0.0000000000000000, 0.0037196691029297,
        0.0000000000000000, 0.0000000000000000, -0.0012398897009766,
        0.0000000000000000, 0.0000000000000000, -0.0012398897009766,
        0.0000000000000000, 0.0000000000000000, -0.0012398897009766,
        0.0000000000000000, 0.0000000000000000, 0.0111590073087890,
        0.0000000000000000, 0.0000000000000000, -0.0037196691029297,
        0.0000000000000000, 0.0000000000000000, -0.0037196691029297,
        0.0000000000000000, 0.0000000000000000, -0.0037196691029297,
    },
    {
        -0.0036235166870317, 0.0000000000000000, 0.0000000000000000,
        -0.0034312118552358, 0.0000000000000000, 0.0000000000000000,
        -0.0046243286848487, 0.0000000000000000, 0.0000000000000000,
        -0.0023680360289339, 0.0000000000000000, 0.0000000000000000,
        -0.0003352301190575, 0.0000000000000000, 0.0000000000000000,
        0.0213123242604053, 0.0000000000000000, 0.0000000000000000,
        -0.0033376661125084, 0.0000000000000000, 0.0000000000000000,
        -0.0035923347727893, 0.0000000000000000, 0.0000000000000000,
    },
    {
        0.0000000000000000, -0.0036235166870317, 0.0000000000000000,
        0.0000000000000000, -0.0034312118552358, 0.0000000000000000,
        0.0000000000000000, -0.0046243286848487, 0.0000000000000000,
        0.0000000000000000, -0.0023680360289339, 0.0000000000000000,
        0.0000000000000000, -0.0003352301190575, 0.0000000000000000,
        0.0000000000000000, 0.0213123242604053, 0.0000000000000000,
        0.0000000000000000, -0.0033376661125084, 0.0000000000000000,
        0.0000000000000000, -0.0035923347727893, 0.0000000000000000,
    },
    {
        0.0000000000000000, 0.0000000000000000, -0.0036235166870317,
        0.0000000000000000, 0.0000000000000000, -0.0034312118552358,
        0.0000000000000000, 0.0000000000000000, -0.0046243286848487,
        0.0000000000000000, 0.0000000000000000, -0.0023680360289339,
        0.0000000000000000, 0.0000000000000000, -0.0003352301190575,
        0.0000000000000000, 0.0000000000000000, 0.0213123242604053,
        0.0000000000000000, 0.0000000000000000, -0.0033376661125084,
        0.0000000000000000, 0.0000000000000000, -0.0035923347727893,
    },
    {
        -0.0017402956998850, 0.0000000000000000, 0.0000000000000000,
        -0.0017402956998850, 0.0000000000000000, 0.0000000000000000,
        0.0052208870996551, 0.0000000000000000, 0.0000000000000000,
        -0.0017402956998850, 0.0000000000000000, 0.0000000000000000,
        -0.0052208870996551, 0.0000000000000000, 0.0000000000000000,
        -0.0052208870996551, 0.0000000000000000, 0.0000000000000000,
        0.0156626612989653, 0.0000000000000000, 0.0000000000000000,
        -0.0052208870996551, 0.0000000000000000, 0.0000000000000000,
    },
    {
        0.0000000000000000, -0.0017402956998850, 0.0000000000000000,
        0.0000000000000000, -0.0017402956998850, 0.0000000000000000,
        0.0000000000000000, 0.0052208870996551, 0.0000000000000000,
        0.0000000000000000, -0.0017402956998850, 0.0000000000000000,
        0.0000000000000000, -0.0052208870996551, 0.0000000000000000,
        0.0000000000000000, -0.0052208870996551, 0.0000000000000000,
        0.0000000000000000, 0.0156626612989653, 0.0000000000000000,
        0.0000000000000000, -0.0052208870996551, 0.0000000000000000,
    },
    {
        0.0000000000000000, 0.0000000000000000, -0.0017402956998850,
        0.0000000000000000, 0.0000000000000000, -0.0017402956998850,
        0.0000000000000000, 0.0000000000000000, 0.0052208870996551,
        0.0000000000000000, 0.0000000000000000, -0.0017402956998850,
        0.0000000000000000, 0.0000000000000000, -0.0052208870996551,
        0.0000000000000000, 0.0000000000000000, -0.0052208870996551,
        0.0000000000000000, 0.0000000000000000, 0.0156626612989653,
        0.0000000000000000, 0.0000000000000000, -0.0052208870996551,
    },
    {
        -0.0036235166870317, 0.0000000000000000, 0.0000000000000000,
        -0.0023680360289339, 0.0000000000000000, 0.0000000000000000,
        -0.0046243286848487, 0.0000000000000000, 0.0000000000000000,
        -0.0034312118552358, 0.0000000000000000, 0.0000000000000000,
        -0.0003352301190575, 0.0000000000000000, 0.0000000000000000,
        -0.0035923347727893, 0.0000000000000000, 0.0000000000000000,
        -0.0033376661125084, 0.0000000000000000, 0.0000000000000000,
        0.0213123242604053, 0.0000000000000000, 0.0000000000000000,
    },
    {
        0.0000000000000000, -0.0036235166870317, 0.0000000000000000,
        0.0000000000000000, -0.0023680360289339, 0.0000000000000000,
        0.0000000000000000, -0.0046243286848487, 0.0000000000000000,
        0.0000000000000000, -0.0034312118552358, 0.0000000000000000,
        0.0000000000000000, -0.0003352301190575, 0.0000000000000000,
        0.0000000000000000, -0.0035923347727893, 0.0000000000000000,
        0.0000000000000000, -0.0033376661125084, 0.0000000000000000,
        0.0000000000000000, 0.0213123242604053, 0.0000000000000000,
    },
    {
        0.0000000000000000, 0.0000000000000000, -0.0036235166870317,
        0.0000000000000000, 0.0000000000000000, -0.0023680360289339,
        0.0000000000000000, 0.0000000000000000, -0.0046243286848487,
        0.0000000000000000, 0.0000000000000000, -0.0034312118552358,
        0.0000000000000000, 0.0000000000000000, -0.0003352301190575,
        0.0000000000000000, 0.0000000000000000, -0.0035923347727893,
        0.0000000000000000, 0.0000000000000000, -0.0033376661125084,
        0.0000000000000000, 0.0000000000000000, 0.0213123242604053,
    }
  };
} // nso_2nd

} // hex8_golds
} // anonymous namespace

TEST_F(MomentumKernelHex8Mesh, nso_ke)
{
  fill_mesh_and_init_fields();

  // Setup solution options for default advection kernel
  solnOpts_.meshMotion_ = false;
  solnOpts_.meshDeformation_ = false;
  solnOpts_.externalMeshDeformation_ = false;
  solnOpts_.includeDivU_ = 0.0;

  unit_test_utils::HelperObjects helperObjs(bulk_, stk::topology::HEX_8, 3, partVec_[0]);

  // Initialize the kernel
  std::unique_ptr<sierra::nalu::Kernel> kernel(
    new sierra::nalu::MomentumNSOKeElemKernel<sierra::nalu::AlgTraitsHex8>(
      bulk_, solnOpts_, velocity_, dudx_, 0.0,
      helperObjs.assembleElemSolverAlg->dataNeededByKernels_));

  // Add to kernels to be tested
  helperObjs.assembleElemSolverAlg->activeKernels_.push_back(kernel.get());

  // Populate LHS and RHS
  helperObjs.assembleElemSolverAlg->execute();

  EXPECT_EQ(helperObjs.linsys->lhs_.extent(0), 24u);
  EXPECT_EQ(helperObjs.linsys->lhs_.extent(1), 24u);
  EXPECT_EQ(helperObjs.linsys->rhs_.extent(0), 24u);

  namespace gold_values = ::hex8_golds::nso_ke;
  unit_test_kernel_utils::expect_all_near(helperObjs.linsys->rhs_,gold_values::rhs);
  unit_test_kernel_utils::expect_all_near<24>(helperObjs.linsys->lhs_, gold_values::lhs);
}

